const semverReg =
  /^(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)(?:-((?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*)(?:\.(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*))*))?(?:\+([0-9a-zA-Z-]+(?:\.[0-9a-zA-Z-]+)*))?$/iu;

function semverCheck(context, danger) {
  const { file } = context || {};
  const { diff } = file || {};

  if (diff?.version) {
    const before = (diff.version.before || "0.0.0").match(semverReg);
    const after = (diff.version.after || diff.version.add).match(semverReg);
    const bPatch = parseInt(before[3], 10);
    const aPatch = parseInt(after[3], 10);
    if (
      before.length < 6 ||
      after.length < 6 ||
      Number(before[1]) > Number(after[1]) ||
      Number(before[2]) > Number(after[2]) ||
      (!after[4] ? bPatch >= aPatch : bPatch > aPatch) ||
      (before[4] &&
        after[4] &&
        parseInt(before[4], 10) >= parseInt(after[4], 10))
    ) {
      danger.logger("warn", {
        message: danger.formatMessages(
          `版本\`${before[0]}\` -> \`${after[0]}\`不符合版本检查标准。请检查：`,
          [
            "- [ ] [regex](https://regex101.com/r/vkijKf/1/)",
            "- [ ] [ts playground](https://www.typescriptlang.org/play?#code/MYewdgzgLgBBCmBbAbvATgJXgcxgXgCgYYB6APQAoAGAHwG0BGAWgE4BdAHQBMAqASg4A6avWbtu-ISMatOvPhQD8ALiYUly2jPG8aEugEMmALwCCTAFpM2dKqyNnL1-hqkqtYuTz29DJ81Y2diwOAc58fPx8iq4A1BS29v5ObPEqQokhyYGxkRGKACQkAJYArgDcBAQAZqVgwFDF4DCgiAAOBmjwFABG8NUgXQBqynBQaMVg2AA0MAbVUOgjYxNTfDAA3kTEpCQwAAJQEEzF2GCD8NvEoJCwfQNdo1jYAKIAHm0AsgZQwAAWpjQaAMAE9iHgYL1+hchjAaDQYAAiKiCFFURF8QSIH7-CgIFDoZ58So7XYHI4nM4XK4tcDQOYLdBPHDvL44gFA0H4SHzRZoIaY7G-P54pCoTA4Yk0m70noABXZ3I6aAQAEkwFAoQ94HQAMxsWYMKhS0ky2AGBXCpWdNUaii89B6g0wI0mnY04rVSE04j3C6CAA28CmUD+MAAPDAAGxwmg+hl8wPB7ChiPR2PxgBypUQfTQWoujDY6wAfDBs7n0PbGWgi+t4Vmc3mC106AAmYswMsV5sO2sd+tx0nECgAQj7dAALGwYIoYPLFSWIRbFaMF1ayyvhYP4y2ddOYAAyQ-x4gTg-H08wZW2zV+1vTw3GrsQm-wdWa8-O118GnrLbDrSkAgEGggAO6dGAFCIhBaBgIiswAYBMCIPAEAQAY2DwKMAAGgASToANOYcDhBQbPeOpUGwAC+xEwEwZbEaRE6UTROGALBygBk3oAECqEYAAxaAKfmgDgFoAY4qAEAMgDv0XQAlsBQfxQFAbQQMoJAkF0WFvEaDCCK0qkkMgADWxQAFYANLVCQDAkHwOHTPGVFusQVEEE5BCtMq3QAOQAJogKUaDztCXQwEM6AQE0YAebM3m+f5fbBeg4Ueb+QA)",
          ]
        ),
      });
    }
  }
}

loadConfig({
  branchCheck: { targets: ["master", "develop"] },
  lockfileCheck: false,
  protectedFilesCheck: [
    {
      file: "README.md",
      reason: "请看 hera 的 /docs/guide/npm 规范 目录",
      level: "fail",
    },
    ".gitignore",
    "lerna.json",
    "tsconfig.json",
    "tsconfig.settings.json",
    /(?:.*)\/\.prettierrc/i,
    /(?:.*)\/\.prettierignore/i,
    /(?:.*)\/\.gitignore/i,
    /(?:.*)\/\.eslintrc/i,
    /(?:.*)\/\.eslintignore/i,
    /(?:.*)\/\.editorconfig/i,
  ],
  packageJsonCheck: [
    {
      custom: semverCheck,
      logChange: true,
      pkgJsonPath: /(?:.\/)?packages\/[A-z\-]*?\/package\.json/iu,
    },
    {
      noBeta: true,
      logChange: true,
    },
    {
      custom: function (context, danger) {
        const { file = {} } = context || {};
        const { diff } = file || {};
        if (diff) {
          danger.logger("info", { message: `${file.name}有变动` });
        }
      },
      pkgJsonPath: /(?:.\/)?packages\/ui(?:-next)?\/package(?:-lock)?\.json/iu,
    },
  ],
});
